package com.denlaku.longan.controller.impl;

import com.denlaku.longan.AppException;
import com.denlaku.longan.http.Resp;
import com.denlaku.longan.http.Return;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author tianx
 */
@RestController
@RequestMapping("/error")
@Slf4j
public class DefaultErrorController extends AbstractErrorController {
    public DefaultErrorController(ErrorAttributes errorAttributes) {
        super(errorAttributes);
    }

    @RequestMapping
    public <T> ResponseEntity<Return<T>> error(Exception e) {
        Return<T> body;
        if (e instanceof AppException) {
            body = Resp.error(e.getMessage());
        } else {
            body = Resp.error("请求异常");
        }
        log.error("request error", e);
        return new ResponseEntity<>(body, HttpStatus.OK);
    }

}
